package com.mtsmda.chapter3.arrayEx.sort;

public class Sorter {
	
	int [] sort;
	
	public Sorter(int [] sort) {
		this.sort = sort;
	}
	
	private void swap(int ar[], int index){
		int temp = ar[index - 1];
		ar[index - 1] = ar[index];
		ar[index] = temp;
	}
	
	public int [] bubleSort(){
		int [] res = this.sort;
		
		for(int i = 1; i < res.length; i++){
			for(int j = res.length - 1; j >= i; j--){
				if(res[j] > res[j - 1]){
					swap(res, j);
				}
			}
		}
		
		return res;
	}
	
	public int [] bubleSort2(){
		int [] res = this.sort;
		
		for(int i = 1; i < res.length; i++){
			for(int j = i; j <= res.length - 1; j++){
				if(res[j] > res[j - 1]){
					swap(res, j);
				}
			}
		}
		
		return res;
	}
	
	
	public int [] selectSort(){
		int [] res = this.sort;
		int index = 0;
		int min = 0;
		
		for(int i = 0; i < res.length; i++){
			index = i;
			min = res[i];
			for(int j = i + 1; j < res.length; j++){
				if(res[j] < min){
					min = res[j];
					index = j;
				}
			}
			
			if(res[i] != res[index]){
				res[index] = res[i];
				res[i] = min;
			}
		}
		
		return res;
	}
	
}